package org.example;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

public class Test {
    public static void main(String[] args) throws NoSuchAlgorithmException, IOException, InterruptedException, KeyManagementException {
        String url="http://h5.gdt.qq.com/xjviewer/default?v=0&tmpl=101&qz_gdt=zthpts2hhngj601&click_ext=eyJhdWlkIjoiMzY1MTUyNDUiLCJsb25nX3Bvc19pZCI6IjEwMTYzMzU4MjYwNTA3NzUiLCJtZWRpdW1faWQiOiI4MDIwMDQyNDAyMzA0NSJ9&gdt_ad_id=13378282667&aidx=2094154278&android_app_platform=2&ad_id=13378287419&gdt_product_id=2000016624&gdt_subordinate_product_id=0%3B5242324&packageName=com.sswl.hjqj5zgdt&versionCode=1&gdt_media_id=80200424023045&siteset=15&xj_ui=2_954c627b9aa32643_02da&no_yyb=1&clklpp=__CLICK_LPP__";

        System.out.println(statusCode(url));
    }
    public static int statusCode(String urls) throws NoSuchAlgorithmException, KeyManagementException, IOException, InterruptedException {
        Thread.sleep(1000);
        if(!(urls.startsWith("https://") || urls.startsWith("http://"))) return 400;
        try {
            URL url = new URL(urls);
            if(url.getProtocol().equals("http")) {
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                int statusCode = connection.getResponseCode();
                return statusCode;
            } else if(url.getProtocol().equals("https")) {
                SSLContext sslContext = SSLContext.getInstance("SSL");
                sslContext.init(null, new TrustManager[]{new X509TrustManager() {
                    public X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }
                    public void checkClientTrusted(X509Certificate[] certs, String authType) {
                    }
                    public void checkServerTrusted(X509Certificate[] certs, String authType) {
                    }
                }}, new SecureRandom());
                HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
                HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
                connection.setHostnameVerifier((hostname, session) -> true);
                int statusCode = connection.getResponseCode();
                return statusCode;
            } else {
                return 400;
            }
        } catch (MalformedURLException e) {
            return 400;
        }
    }
}
